#!/usr/bin/perl -w
use strict;
use Getopt::Long;
my $windowsize = 500;
my $index = 'CRI';
my $cutoff = 0;
GetOptions('w|window:s' => \$windowsize,
	   'i|index:s'  => \$index,
	   'c|cutoff:s' => \$cutoff,
	   );

for my $file ( @ARGV ) {
    open(my $fh => $file) || die "can't open $file: $!";
    my $header = <$fh>;
    chomp($header);
    my @cols = split(/\s+/,$header);
    my $i = 0;
    my %m = map { $_ => $i++ } @cols;
    if( ! exists $m{$index} ) {
	die("unknown index $index requested\n");
    }
    my $total_windows = 0;
    my $rip_windows = 0;
    while(<$fh>) {
	my @row = split;
	my ($chrom,$idx) = map { $row[ $m{$_} ] } ('CHROM',$index);
	if ( $index eq 'CRI' ) {
	  $rip_windows++ if($idx > $cutoff );
	} else {
	  $rip_windows++ if($idx <= $cutoff );
	}
	$total_windows++;
    }
    printf "%s\t%d windows out of %d total -- %.2f%% RIPed\n",
    $file,$rip_windows, $total_windows, 
    100 * $rip_windows /$total_windows;
}
